﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;

namespace GtsServer.DAL
{
    /// <summary>
    /// 数据访问类:Order
    /// </summary>
    public partial class OrderDAL
    {
        public OrderDAL()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string OrderAccount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Order");
            strSql.Append(" where OrderAccount=@OrderAccount ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderAccount", SqlDbType.VarChar,50)};
            parameters[0].Value = OrderAccount;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "Order");
        }

        public DataSet GetRechargeRecord(string userAccount)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount)
            };

            DataSet ds = DbHelperSQL.RunProcedure("GetRechargeRecord", parms, "GetRechargeRecord");
            return ds;
        }

        /// <summary>
        /// 添加order
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="netType">联网请求方式 0：计费请求；1：timer请求；</param>
        /// <param name="chargeMode">计费方式：1：支付宝:；2：神州付； 3：短信； 4：银联</param>
        /// <param name="appID">游戏的id</param>
        /// <param name="prizeID">物品id</param>
        /// <param name="gameType">游戏模式 1：经典模式 2：比赛模式 3：彩池模式</param>
        /// <param name="taskID">任务（比赛）id</param>
        /// <param name="price">金额</param>
        /// <returns></returns>
        public DataSet AddOrder(string userAccount, int netType, int chargeMode, int appID, int prizeID, int gameType, int taskID, decimal price, int pid)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@netType",netType),
                new SqlParameter("@chargeMode",chargeMode),
                new SqlParameter("@appID",appID),
                new SqlParameter("@prizeID",prizeID),
                new SqlParameter("@gameType",gameType),
                new SqlParameter("@taskID",taskID),
                new SqlParameter("@price",price),
                new SqlParameter("@pid",pid)
            };
            DataSet ds = DbHelperSQL.RunProcedure("AddOrder", parms, "AddOrder");
            return ds;
        }

        /// <summary>
        /// 添加order
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="netType">联网请求方式 0：计费请求；1：timer请求；</param>
        /// <param name="chargeMode">计费方式：1：支付宝:；2：神州付； 3：短信； 4：银联</param>
        /// <param name="appID">游戏的id</param>
        /// <param name="prizeID">物品id</param>
        /// <param name="gameType">游戏模式 1：经典模式 2：比赛模式 3：彩池模式</param>
        /// <param name="taskID">任务（比赛）id</param>
        /// <param name="price">金额</param>
        /// <param name="pid"></param>
        /// <param name="carNum">卡号</param>
        /// <param name="pwd">密码</param>
        /// <param name="szfPrice">面值</param>
        /// <param name="op">运营商  1：中国移动 2：中国联通 3：中国电信</param>
        /// <returns></returns>
        public DataSet AddOrderSzf(string userAccount, int netType, int chargeMode, int appID, int prizeID, int gameType, int taskID, decimal price, int pid, string carNum, string pwd, decimal szfPrice, int op)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@netType",netType),
                new SqlParameter("@chargeMode",chargeMode),
                new SqlParameter("@appID",appID),
                new SqlParameter("@prizeID",prizeID),
                new SqlParameter("@gameType",gameType),
                new SqlParameter("@taskID",taskID),
                new SqlParameter("@price",price),
                new SqlParameter("@pid",pid),
                new SqlParameter("@card",carNum),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@szfPrice",szfPrice),
                new SqlParameter("@op",op)
            };
            DataSet ds = DbHelperSQL.RunProcedure("AddOrderSzf", parms, "AddOrderSzf");
            return ds;
        }

        /// <summary>
        /// 添加order
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="netType">联网请求方式 0：计费请求；1：timer请求；</param>
        /// <param name="chargeMode">计费方式：1：支付宝:；2：神州付； 3：短信； 4：银联</param>
        /// <param name="appID">游戏的id</param>
        /// <param name="prizeID">物品id</param>
        /// <param name="gameType">游戏模式 1：经典模式 2：比赛模式 3：彩池模式</param>
        /// <param name="taskID">任务（比赛）id</param>
        /// <param name="price">金额</param>
        /// <returns></returns>
        public DataSet AddCommonOrder(string userAccount, int netType, int chargeMode, int appID, int prizeID, int gameType, int taskID, decimal price, int pid, string orderAccount)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@netType",netType),
                new SqlParameter("@chargeMode",chargeMode),
                new SqlParameter("@appID",appID),
                new SqlParameter("@prizeID",prizeID),
                new SqlParameter("@gameType",gameType),
                new SqlParameter("@taskID",taskID),
                new SqlParameter("@price",price),
                new SqlParameter("@pid",pid),
                new SqlParameter("@orderCommonAccount",orderAccount)
            };
            DataSet ds = DbHelperSQL.RunProcedure("AddCommonOrder", parms, "AddCommonOrder");
            return ds;
        }

        /// <summary>
        /// 添加order
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="netType">联网请求方式 0：计费请求；1：timer请求；</param>
        /// <param name="chargeMode">计费方式：1：支付宝:；2：神州付； 3：短信； 4：银联</param>
        /// <param name="appID">游戏的id</param>
        /// <param name="prizeID">物品id</param>
        /// <param name="gameType">游戏模式 1：经典模式 2：比赛模式 3：彩池模式</param>
        /// <param name="taskID">任务（比赛）id</param>
        /// <param name="price">金额</param>
        /// <param name="pid"></param>
        /// <param name="carNum">卡号</param>
        /// <param name="pwd">密码</param>
        /// <param name="szfPrice">面值</param>
        /// <param name="op">运营商  1：中国移动 2：中国联通 3：中国电信</param>
        /// <returns></returns>
        public DataSet AddCommonOrderSzf(string userAccount, int netType, int chargeMode, int appID, int prizeID, int gameType, int taskID, decimal price, int pid, string carNum, string pwd, decimal szfPrice, int op,string orderAccount)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@netType",netType),
                new SqlParameter("@chargeMode",chargeMode),
                new SqlParameter("@appID",appID),
                new SqlParameter("@prizeID",prizeID),
                new SqlParameter("@gameType",gameType),
                new SqlParameter("@taskID",taskID),
                new SqlParameter("@price",price),
                new SqlParameter("@pid",pid),
                new SqlParameter("@card",carNum),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@szfPrice",szfPrice),
                new SqlParameter("@op",op),
                new SqlParameter("@orderCommonAccount",orderAccount)
            };
            DataSet ds = DbHelperSQL.RunProcedure("AddCommonOrderSzf", parms, "AddCommonOrderSzf");
            return ds;
        }


        public void UpdateOrderStutas(string orderAccount, int stutas)
        {

            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@orderAccount",orderAccount),
                new SqlParameter("@stutas",stutas)
            };
            DbHelperSQL.RunProcedure("EditOrderStutas", parms, "EditOrderStutas");
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.Order model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Order set ");
            strSql.Append("ID=@ID,");
            strSql.Append("ExpenseID=@ExpenseID,");
            strSql.Append("Price=@Price,");
            strSql.Append("State=@State,");
            strSql.Append("PayTime=@PayTime,");
            strSql.Append("PrizeID=@PrizeID");
            strSql.Append(" where OrderAccount=@OrderAccount ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@ExpenseID", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@PayTime", SqlDbType.DateTime),
					new SqlParameter("@PrizeID", SqlDbType.Int,4),
					new SqlParameter("@OrderAccount", SqlDbType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ExpenseID;
            parameters[2].Value = model.Price;
            parameters[3].Value = model.State;
            parameters[4].Value = model.PayTime;
            parameters[5].Value = model.PrizeID;
            parameters[6].Value = model.OrderAccount;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string OrderAccount)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Order ");
            strSql.Append(" where OrderAccount=@OrderAccount ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderAccount", SqlDbType.VarChar,50)};
            parameters[0].Value = OrderAccount;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string OrderAccountlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Order ");
            strSql.Append(" where OrderAccount in (" + OrderAccountlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GtsServer.Model.Order GetModel(string OrderAccount)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 OrderAccount,ID,ExpenseID,Price,State,PayTime,PrizeID from Order ");
            strSql.Append(" where OrderAccount=@OrderAccount ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderAccount", SqlDbType.VarChar,50)};
            parameters[0].Value = OrderAccount;

            GtsServer.Model.Order model = new GtsServer.Model.Order();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["OrderAccount"] != null && ds.Tables[0].Rows[0]["OrderAccount"].ToString() != "")
                {
                    model.OrderAccount = ds.Tables[0].Rows[0]["OrderAccount"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ExpenseID"] != null && ds.Tables[0].Rows[0]["ExpenseID"].ToString() != "")
                {
                    model.ExpenseID = int.Parse(ds.Tables[0].Rows[0]["ExpenseID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Price"] != null && ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["State"] != null && ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PayTime"] != null && ds.Tables[0].Rows[0]["PayTime"].ToString() != "")
                {
                    model.PayTime = DateTime.Parse(ds.Tables[0].Rows[0]["PayTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PrizeID"] != null && ds.Tables[0].Rows[0]["PrizeID"].ToString() != "")
                {
                    model.PrizeID = int.Parse(ds.Tables[0].Rows[0]["PrizeID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select OrderAccount,ID,ExpenseID,Price,State,PayTime,PrizeID ");
            strSql.Append(" FROM Order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" OrderAccount,ID,ExpenseID,Price,State,PayTime,PrizeID ");
            strSql.Append(" FROM Order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Order";
            parameters[1].Value = "OrderAccount";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

